﻿<template>
    <el-col :sm="24" :md="pzoption.mdwidth" v-show="ishide">
        <i class="iconfont icon-shezhi pull-right widgetset hidden-print" @click.stop="dialogInputVisible = true"></i>
        <i class="iconfont icon-shanchu pull-right widgetdel hidden-print" @click.stop="delWid(pzoption.wigdetcode)"></i>
        <el-form-item :label="pzoption.title" :prop="'wigetitems.' + index + '.value'" :rules="childpro.rules">
            <el-input v-model="pzoption.value" style="display:none">
            </el-input>


            <el-select v-model="childpro.svalue" style="width:100%" v-show="!childpro.disabled" v-if="!childpro.multiple" :disabled="childpro.disabled" filterable clearable>
                <el-option v-for="(item, index) in pzoption.dataset" v-if="pzoption.wdlist.length==1"
                           :key="item[pzoption.wdlist[0].colid]+''"
                           :label="item[pzoption.wdlist[0].colid]"
                           :value="item[pzoption.wdlist[0].colid]+''">
                </el-option>

                <el-option v-for="(item, index) in pzoption.dataset" v-if="pzoption.wdlist.length>1"
                           :key="index + item[pzoption.wdlist[1].colid]+''"
                           :label="item[pzoption.wdlist[0].colid]"
                           :value="item[pzoption.wdlist[1].colid]+''">
                </el-option>
            </el-select>
            <el-select v-model="childpro.mvalue" style="width:100%"  v-if="childpro.multiple" :disabled="childpro.disabled" multiple filterable clearable>
                <el-option v-for="(item, index) in pzoption.dataset" v-if="pzoption.wdlist.length==1"
                           :key="item[pzoption.wdlist[0].colid]+''"
                           :label="item[pzoption.wdlist[0].colid]"
                           :value="item[pzoption.wdlist[0].colid]+''">
                </el-option>

                <el-option v-for="(item, index) in pzoption.dataset" v-if="pzoption.wdlist.length>1"
                           :key="index + item[pzoption.wdlist[1].colid]+''"
                           :label="item[pzoption.wdlist[0].colid]"
                           :value="item[pzoption.wdlist[1].colid]+''">
                </el-option>
            </el-select>
            <div class="disdiv" v-show="childpro.disabled&&!childpro.multiple" v-html="pzoption.valuetext">

            </div>
        </el-form-item>
        <el-dialog title="组件属性" :visible.sync="dialogInputVisible" width="30%">

            <el-form :model="childpro">
                <el-form-item label="必填">
                    <el-switch v-model="childpro.rules.required"></el-switch>
                </el-form-item>
                <el-form-item label="是否多选">
                    <el-switch v-model="childpro.multiple" size="mini" style="width:100%">
                    </el-switch>
                </el-form-item>
                <el-form-item label="默认值">
                    <el-input v-model="childpro.defval" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="下拉框更改时执行代码">
                    <el-input v-model="childpro.jscode" :autosize="{ minRows: 6, maxRows: 9}" autocomplete="off" type="textarea"></el-input>
                </el-form-item>
            </el-form>
        </el-dialog>
    </el-col>

</template>
<style>
    .el-input,.is-disabled >.el-input__inner {
        border: 0px;
        background: TRANSPARENT !important;
    }
</style>
<script>
    module.exports = {
        props: ['pzoption', 'index'],
        data: function () {
            return {
                dialogInputVisible: false,
                childpro: {
                    placeholder: "请选择",
                    hidefiled: false,
                    hide: false,
                    disabled: false,
                    itemtype: "text",
                    defval: "",
                    mvalue: [],
                    svalue: "",
                    jscode: "",
                    multiple: false,
                    rules: {
                        required: false, message: '不能为空', type: "string"
                    }
                }
            }
        },
        computed: {
            ishide() {
                return !this.childpro.hide || window.location.href.indexOf('FormBuildV1') > -1
            }
        },
        methods: {
            delWid: function (wigdetcode) {
                // 子组件中触发父组件方法ee并传值cc12345
                this.$root.nowwidget = {};
                _.remove(this.$root.FormData.wigetitems, function (obj) {
                    return obj.wigdetcode == wigdetcode;
                });
            },
            selchange: function () {
                try {
                    var data = JSON.parse(JSON.stringify(this.pzoption));
                    let jscode = this.childpro.jscode;
                    let func = new Function('data', jscode);
                    func(data)
                } catch (e) {
                    app.$notify({
                        title: '成功',
                        message: '解析JS代码有误',
                        type: 'success'
                    });
                }

            }
        },
        mounted: function () {
            var pro = this;
            pro.$nextTick(function () {
                if (pro.$root.addchildwig) {
                    pro.$root.addchildwig();//不能缺少,dom加载完成
                }
                if (pro.pzoption.childpro.itemtype) {
                    pro.childpro = pro.pzoption.childpro;
                    pro.$root.UpdateYBData(pro.pzoption, function (ybdata) {
                        if (app.isview) {//添加页面
                            pro.pzoption.value = app.jxparam(pro.childpro.defval);
                        }

                    });
                } else {
                    if (pro.pzoption.datatype == '2') {
                        pro.pzoption.staticdata = JSON.stringify([
                            { 'lable': '一月', 'val': "1" },
                            { 'lable': '二月', 'val': "2" },
                            { 'lable': '三月', 'val': "3" },
                            { 'lable': '四月', 'val': "4" }
                        ])
                        pro.$root.jxfiled(pro.pzoption)
                        pro.$root.addwd("lable", pro.pzoption)
                        pro.$root.addwd("val", pro.pzoption)
                    }
                    pro.pzoption.childpro = pro.childpro;
                }



            })

        },
        watch: {
            'pzoption.value': { //深度监听，可监听到对象、数组的变化
                handler(newV, oldV) {
                    if (newV) {
                        var chi = this;
                        if (chi.pzoption.childpro.multiple) {
                            chi.pzoption.childpro.mvalue = newV.split(',');
                            chi.childpro.mvalue = newV.split(',');

                        } else {
                            chi.pzoption.childpro.svalue = newV + "";//为了变成字符
                            chi.childpro.svalue = newV + "";//为了变成字符

                        }
                    }
                },
                deep: true,
                immediate: true//加载初始值
            },
            'childpro.mvalue': { //深度监听，可监听到对象、数组的变化
                handler(newV, oldV) {
                    if (newV) {
                        var chi = this;
                        if (chi.pzoption.childpro.multiple) {
                            chi.pzoption.value = newV.join();
                            var tempuser =[];
                            _.forEach(newV, function (obj) {
                                _.forEach(chi.pzoption.dataset, function (item) {
                                    if (item[chi.pzoption.wdlist[0].colid] == obj || item[chi.pzoption.wdlist[1].colid] == obj) {
                                        tempuser.push(item[chi.pzoption.wdlist[0].colid])
                                    }
                                })
                            })
                            try {
                                var data = JSON.parse(JSON.stringify(chi.pzoption));
                                let jscode = chi.childpro.jscode;
                                let func = new Function('data', jscode);
                                func(data)
                            } catch (e) {
                                app.$notify({
                                    title: '成功',
                                    message: '解析JS代码有误',
                                    type: 'success'
                                });
                            }
                            chi.pzoption.valuetext = tempuser.join()
                        }
                        //多选valuetext没空搞
                    }
                },
                deep: true
            },
            'childpro.svalue': { //深度监听，可监听到对象、数组的变化
                handler(newV, oldV) {
                    var chi = this;
                    if (newV) {
                        if (!chi.pzoption.childpro.multiple) {
                            chi.pzoption.value = newV + "";
                            _.forEach(chi.pzoption.dataset, function (item) {
                                if (item[chi.pzoption.wdlist[0].colid] == chi.pzoption.value || item[chi.pzoption.wdlist[1].colid] == chi.pzoption.value) {
                                    chi.pzoption.valuetext = item[chi.pzoption.wdlist[0].colid];
                                }
                            })
                            try {
                                var data = JSON.parse(JSON.stringify(chi.pzoption));
                                let jscode = chi.childpro.jscode;
                                let func = new Function('data', jscode);
                                func(data)
                            } catch (e) {
                                app.$notify({
                                    title: '成功',
                                    message: '解析JS代码有误',
                                    type: 'success'
                                });
                            }
                        }
                    }
                },
                deep: true
            }

        }
    };
</script>